package com.dev.receiver;

import com.dev.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.IntentParams;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.wifi.WifiEvents;

//构建消息接收者/注册者
public class WifiEventSubscriber extends CommonEventSubscriber {

    private AbilitySlice slice;

    public WifiEventSubscriber(CommonEventSubscribeInfo info, AbilitySlice slice) {
        super(info);
        this.slice = slice;
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        if (commonEventData == null || commonEventData.getIntent() == null) {
            return;
        }
        if (WifiEvents.EVENT_ACTIVE_STATE.equals(commonEventData.getIntent().getAction())) {
            //获取附带参数
            IntentParams params = commonEventData.getIntent().getParams();
            if (params == null) {
                return;
            }
            int wifiState = (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);
            if (wifiState == WifiEvents.STATE_CONNECTED) {
                //处理wifi被打开消息
                new ToastDialog(slice).setText("wifi open!").show();
                LogUtils.info("WIFI", "wifi is on!");
            } else if (wifiState == WifiEvents.STATE_DISCONNECTED) {
                //处理wifi被关闭的消息
                new ToastDialog(slice).setText("wifi close!").show();
                LogUtils.info("WIFI", "wifi is off!");
            }
        }
    }
}